package com.example.Set;

import java.util.HashSet;
import java.util.Iterator;

public class TestHashSet {
    public static void main(String[] args) {
        //method();
        //method2();
        HashSet<Student> hs = new HashSet<>();

        Student s1 = new Student("xiaohei", 19);
        Student s2 = new Student("xiaohei", 19);
        Student s3 = new Student("xiaomei", 18);

        hs.add(s1);
        hs.add(s2);
        hs.add(s3);

        for (Student student : hs) {
            System.out.println(student);
        }
//        Student{name='xiaomei', age=18}
//        Student{name='xiaohei', age=19}


    }

    private static void method2() {
        Student s1 = new Student("xiaozhi", 22);
        Student s2 = new Student("xiaomei", 21);

        //因为在Obiect类中，是根据对象的地址值计算出来的哈希值
        System.out.println(s1.hashCode());  //509886383 //265724890
        System.out.println(s1.hashCode());  //509886383 //265724890
        System.out.println(s2.hashCode());  //1854778591 //265334723
    }

    private static void method() {
        HashSet<String> hs = new HashSet<>();

        hs.add("hello");
        hs.add("world");
        hs.add("java");
        hs.add("java");
        hs.add("java");

        Iterator<String> it = hs.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("-----------");
        for (String h : hs) {
            System.out.println(h);
        }
    }
}
